package com.aat.dxfy.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Test02 {

	public static String convert(String utfstr) {
		StringBuilder sb = new StringBuilder();
		int i = -1;
		int pos = 0;
		while ((i = utfstr.indexOf("\\u", pos)) != -1) {
			sb.append(utfstr.substring(pos, i));
			if (i + 5 < utfstr.length()) {
				pos = i + 6;
				sb.append((char) Integer.parseInt(utfstr.substring(i + 2, i + 6), 16));
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("http://ip.taobao.com/service/getIpInfo.php?ip=124.126.17.179");
		CloseableHttpResponse response1 = null;

		try {
			httpGet.addHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0");
			httpGet.addHeader("Accept-Encoding", "gzip, deflate");

			response1 = httpclient.execute(httpGet);
			System.out.println(response1.getStatusLine());
			HttpEntity entity1 = response1.getEntity();
			String res = EntityUtils.toString(entity1);
			System.out.println(res);
			EntityUtils.consume(entity1);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				response1.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
